package cn.edu.zafu.bigdata.smj.c;
import java.io.*;
import java.util.*;

public class StudentManagementSystem {
    private static final String FILE_NAME = "students.dat";
    private static Map<String, Student> studentMap = new HashMap<>();

    public static void main(String[] args) {
        loadStudents();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    addStudent(scanner);
                    break;
                case "2":
                    searchStudent(scanner);
                    break;
                case "3":
                    updateStudent(scanner);
                    break;
                case "4":
                    deleteStudent(scanner);
                    break;
                case "5":
                    saveStudents();
                    System.out.println("程序已退出。");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
            }
        }
    }

    private static void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentMap = (Map<String, Student>) ois.readObject();
        } catch (FileNotFoundException e) {
            System.out.println("学生信息文件不存在，已创建新文件。");
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载学生信息时出错：" + e.getMessage());
        }
    }

    private static void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(studentMap);
            System.out.println("学生信息已保存。");
        } catch (IOException e) {
            System.out.println("保存学生信息时出错：" + e.getMessage());
        }
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (studentMap.containsKey(id)) {
            System.out.println("学号已存在，请重新输入。");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        studentMap.put(id, new Student(id, name, phone));
        System.out.println("学生信息已添加。");
    }

    private static void searchStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要检索的学号 (输入 'QUIT' 返回上一级): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) break;
            Student student = studentMap.get(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要更新的学号 (输入 'QUIT' 返回上一级): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) break;
            Student student = studentMap.get(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                System.out.println("学生信息已更新。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要删除的学号 (输入 'QUIT' 返回上一级): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) break;
            if (studentMap.remove(id) != null) {
                System.out.println("学生信息已删除。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }
}

class Student implements Serializable {
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public String getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{id='" + id + "', name='" + name + "', phone='" + phone + "'}";
    }
}